<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Report extends CI_Controller {
	public function __construct() {
		parent::__construct();
		$this->load->helper(array('form', 'url'));
		$this->load->database();
		$this->headerdata = array();
		if (($this->session->userdata('userid')>0) && ($this->session->userdata('hash')==md5($this->session->userdata('userid').$this->session->userdata('username')))){
			$this->headerdata['userid'] = $this->session->userdata('userid');
			$this->headerdata['username'] = $this->session->userdata('username');
		}else{
			$this->headerdata['userid'] = 0;
			$this->headerdata['username'] = 'Guest';
		}
		
		$this->headerdata['route'] = 'localhost/source';
		$this->headerdata['routelink'] = '';
		$this->headerdata['route2'] = 'Báo cáo';
		$this->headerdata['route2link'] = 'report';

		if ($this->session->userdata('userid')==0) redirect(site_url());

		if (($this->session->userdata('msg') != '') && ($this->session->userdata('msgtype') != '')){
			$this->headerdata['msg'] = $this->session->userdata('msg');
			$this->headerdata['msgtype'] = $this->session->userdata('msgtype');
			$this->session->set_userdata('msg','');
			$this->session->set_userdata('msgtype','');
		}
	}

    public function newreport(){
    	if ($this->headerdata['userid'] == 0) redirect(site_url());
	    $success = "";
	    $error = "";
	    $data = array();

	    $uid = $this->session->userdata('userid');
	    $this->load->model('UserModel');
		$type = $this->UserModel->gettype($this->session->userdata('userid'), $this->session->userdata('username'));
		$data['type'] = intval($type);
	    $this->load->model('ReportModel');
	    $this->db->where('student_id',$uid);
		$q = $this->db->get('report');
		$data['report'] = $q->result();

	    if($this->input->post('tittle')!='' && $this->input->post('useremail')!='' && $this->input->post('content') != '') {
	    	$config['upload_path'] = './upload/';
        	$config['allowed_types'] = 'gif|jpg|png|doc|txt|zip|rar|docx|xls|xlsx|pdf';
        	$config['max_size']  = 1024 * 8;
        	$config['encrypt_name'] = TRUE;
        	$this->load->library("upload",$config);
	    
        	//get id email
        	$this->load->model('GuideModel');
			$data['type'] = $this->GuideModel->gettype($uid);
			$data['teacher'] = null;
			$data['company'] = null;

			if($data['type'] == 1) {
				$q1 = $this->db->query("SELECT * from manage_student where student_id= $uid");
				$teacher_id = $q1->first_row()->teacher_id;
				$company_id = $q1->first_row()->company_id;
			} else if($data['type'] == 0){
				$q1 = $this->db->query("SELECT * from manage_student where teacher_id= $uid");
				$student_id = $q1->first_row()->student_id;
			}

		    if($this->upload->do_upload('file') != null){
		    	//$success= "Upload thành công";
		    	$data1 = $this->upload->data();
		    	$info = array("file"=> $data1['file_name'],
		    				"student_id" => $this->session->userdata('userid'),
		    				"teacher_id" => $teacher_id,
		    				"company_id" => $company_id,
		    				"tittle"=> $this->input->post('tittle'),
		    				"content"=> $this->input->post('content'),
		    				"time"=>time()
		    		);
		    	$fid = $this->ReportModel->add($info);
		    	if($fid) {
		    		$success 	= "Báo cáo được gửi thành công.";
		    		//$error 		= "File successfully uploaded";
		    	} else{
		    		//$success 	= "error";
		    		$error 		= "Tải file lỗi. Hãy thử lại!"; 
		    	}
		    	
		    	$data['success']= $success;
		    	$data['error']	= $error;
		    	$this->headerdata['route2'] = 'Gửi báo cáo thành công.';
		    }
		}else {
			$data['success'] =$success;
			$error = "Hãy nhập đầy đủ nội dung báo cáo!!!!";
			$data['error'] = $error;
			$this->headerdata['route2'] = 'Gửi lỗi.';
		}
		$this->load->view('common/header',$this->headerdata);
		$this->load->view('common/left', $data);
		$this->load->view('report',$data);
		$this->load->view('common/footer');
	}
	    
	public function download($file)
	{
		$this->load->helper('download');
		$data = file_get_contents('./upload/' . $file); // Read the file's contents
        $name = $file;
        force_download($name, $data);
	}

	public function index()
	{
		$data= array();
		$success= '';
		$error= '';
		$uid= $this->session->userdata('userid');

		$this->load->model('ReportModel');
		$this->load->model('GuideModel');
		$this->load->model('UserModel');
		$type = $this->UserModel->gettype($this->session->userdata('userid'), $this->session->userdata('username'));
		$data['type'] = intval($type);
		//$data['type'] = $this->GuideModel->gettype($uid);
		$data['teacher'] = null;
		$data['company'] = null;

		if($data['type']== 1) {
			$q1= $this->db->query("SELECT * from manage_student where student_id= $uid");
			$teacher_id= $q1->first_row()->teacher_id;
			$company_id= $q1->first_row()->company_id;
			$data['cid']= $q1->first_row()->id;
			
			if($teacher_id != 0) {
				$q2= $this->db->query("SELECT * from user where id= $teacher_id");
				$data['teacher']= $q2->first_row()->user;
			}
			if($company_id != 0) {
				$q3= $this->db->query("SELECT * from company where id= $company_id");
				$data['company']= $q3->first_row()->email;
			}

			$this->db->where('student_id',$uid);
			$q= $this->db->get('report');
		} else if($data['type'] == 0){
			$q1 = $this->db->query("SELECT * from manage_student where teacher_id= $uid");
			$student_id = $q1->first_row()->student_id;
			$data['cid'] = $q1->first_row()->id;

			$this->db->where('teacher_id',$uid);
			$q= $this->db->get('report');
		} else if($data['type'] == 2) {
			$q2 = $this->db->query("SELECT * from manage_student where company_id= $uid");
			$student_id = $q2->first_row()->student_id;
			$data['cid'] = $q2->first_row()->id;

			$this->db->where('company_id',$uid);
			$q= $this->db->get('report');
		}

		//if ($q->num_rows()){
		
		$this->headerdata['route2'] = 'Báo cáo';

		$data['success'] = $success;
		$data['error'] = $error;
		
		$data['report'] = $q->result();

		$this->load->view('common/header',$this->headerdata);
		$this->load->view('common/left', $data);
		$this->load->view('report',$data);
		$this->load->view('common/footer');
		//}
	}

	public function detail($id){
		if (!isset($id) || !is_numeric($id) || ($id < 1)) redirect(site_url());
		
		$data = array();
		$this->load->model('ReportModel');
		$data['report'] = $this->ReportModel->get($id);
		$this->load->model('UserModel');
		$type = $this->UserModel->gettype($this->session->userdata('userid'), $this->session->userdata('username'));
		$data['type'] = intval($type);
		
		$this->headerdata['route3'] = "abc";//$data['report']->title;
		
		$this->load->view('common/header',$this->headerdata);
		$this->load->view('common/left', $data);
		$this->load->view('report_detail',$data);
		$this->load->view('common/footer');
	}

	public function guide()
	{
		$data 	= array();
		$success= '';
		$error	= '';

		$this->load->model('UserModel');
		$type = $this->UserModel->gettype($this->session->userdata('userid'), $this->session->userdata('username'));
		$data['type'] = intval($type);
		$this->load->model('GuideModel');
		$data['a']= $this->GuideModel->get($this->session->userdata('userid'));

		$this->headerdata['route3'] = "abc123";//$data['report']->title;
		
		$this->load->view('common/header',$this->headerdata);
		$this->load->view('common/left', $data);
		$this->load->view('report',$data);
		$this->load->view('common/footer');
	}
}
?>